﻿/**
 * sprite  Base 类
 */
var ast = new Astar();

var sprite = function (){
	this.name;
	this.Stepx;
	this.Stepy;
	this.width;
	this.height;
	this.abx=0;
	this.aby=0;
	this.currentaction;
	this.actions =new Array();
	this.imgs;
	this.isWaitting=false;
	this.isPlayer= false;
	this.attackActions=new Array();//0:待机；1：攻击；2被攻击;3 战斗失败;4战斗胜利
	this.properties = new HashMap();
	
}

sprite.prototype.attact = function (){
	this.currentaction = null;
	this.attackActions[1].step = 0;
	this.actions.push(this.attackActions[1]);
	this.actions.push(this.attackActions[0]);
	
	if (sprites[1]!=this) { //TODO 这里只考虑了两个角色
		sprites[0]=sprites[1];
		sprites[1]=this;
	}
}

sprite.prototype.GetPro = function (proname){
	var exv = SpriteProSystem(proname,this);
	if (exv == -1) exv=this.properties.get(proname);
	return exv;
}

sprite.prototype.attactted = function (){
	this.currentaction = null;
	this.attackActions[2].step = 0;
	this.actions.push(this.attackActions[2]);
	this.actions.push(this.attackActions[0]);
	
}

sprite.prototype.over = function (){
	this.currentaction = null;
	this.attackActions[3].step = 0;
	this.actions.push(this.attackActions[3]);
}

sprite.prototype.vic = function (){
	this.currentaction = null;
	this.attackActions[4].step = 0;
	this.actions.push(this.attackActions[4]);
}

sprite.prototype.act = function (){
	if (this.currentaction == null && this.actions.length ==0){//完全停止了
		return;
	}
	
	if (this.currentaction == null){
		this.currentaction = this.actions.shift();
		this.currentaction.init();
		this.isWaitting =false;
		return;
	}
	this.currentaction.act();
	
	if (this.currentaction != null && this.currentaction.State == 2 && this.actions.length>0){ 
		this.currentaction = this.actions.shift();
		this.currentaction.init();
		this.isWaitting =false;
	} else if (this.currentaction != null && this.currentaction.State == 2) { //除了最后一个保留动作，没有其他的动作了。
		//DoMapEvent只执行一次
		if (this.isPlayer && this.isWaitting==false && this.currentaction.DisableME == false) 
			DoMapEvent(this.Stepx,this.Stepy); 
		this.isWaitting =true;
	}

}

sprite.prototype.go = function (gox,goy){
	ox = this.Stepx; oy = this.Stepy;
	var path = ast.getPath(this.Stepx,this.Stepy,gox,goy) 
	
	if(path!=undefined){
		
		while (this.actions.length>0){//TODO这里是清除所有之前的动作，可能需要有个参数来控制}
			this.actions.pop();
		}
		
		while (path.length>0){
			setGotoIcon(gox,goy);
			var newk = path.pop();
			
			
			act = new walkaction();
			act.sprite = this;
			//act.Eatmsg = true;
			
			var xx = newk[0];
			var yy = newk[1];
			
			if (xx >ox )
			{
				if (yy == oy){
					act.direction =CONST_RIGHT;
					act.animimgs = this.imgs[CONST_RIGHT];
				} else if (yy > oy){
					act.direction =CONST_RIGHT_DOWN;
					act.animimgs = this.imgs[CONST_RIGHT_DOWN];
				} else {
					act.direction =CONST_RIGHT_UP;
					act.animimgs = this.imgs[CONST_RIGHT_UP];
				}
			} else if (xx <ox){
				if (yy==oy){
					act.direction =CONST_LEFT;
					act.animimgs = this.imgs[CONST_LEFT];
				} else if (yy > oy){
					act.direction =CONST_LEFT_DOWN;
					act.animimgs = this.imgs[CONST_LEFT_DOWN];
				} else {
					act.direction =CONST_LEFT_UP;
					act.animimgs = this.imgs[CONST_LEFT_UP];
				}
				
			} else if (yy >oy){
				act.direction =CONST_DOWN;
				act.animimgs = this.imgs[CONST_DOWN];
			} else if (yy <oy){
				act.direction =CONST_UP;
				act.animimgs = this.imgs[CONST_UP];
			}
			ox = xx;oy=yy;
			//act.init();
			
			this.actions.push(act);
		}
		
	}
}

//禁止混淆 Begin
//window['sprite'] = sprite; 
//sprite.prototype['name'] = sprite.prototype.name;
//sprite.prototype['imgs'] = sprite.prototype.imgs;
//sprite.prototype['properties'] = sprite.prototype.properties;
//sprite.prototype['isPlayer'] = sprite.prototype.isPlayer;
//sprite.prototype['Stepx'] = sprite.prototype.Stepx;
//sprite.prototype['Stepy'] = sprite.prototype.Stepy;
//sprite.prototype['go'] = sprite.prototype.go;
//禁止混淆 END

